package com.atguigu.binarysearch;

/**
 * @author xgc
 * @create 2021-07-22 10:47
 * 同步锁（解决线程安全问题）
 */
public class Singleton3 {
    //私有构造
    private Singleton3(){}

    //初始值为null
    private static Singleton3 singleton3 = null;

    public static Singleton3 getInstance(){

        // 等同于 synchronized public static Singleton3 getInstance()
        synchronized (Singleton3.class){
            // 注意：里面的判断是一定要加的，否则出现线程安全问题
            if (singleton3 == null){
                singleton3 = new Singleton3();
            }
            return singleton3;
        }
    }
}
